<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="客户姓名" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="customerPhone">
        <el-input
          v-model="queryParams.customerPhone"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号码" prop="licensePlate">
        <el-input
          v-model="queryParams.licensePlate"
          placeholder="请输入车牌号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="汽车类型" prop="carType">
        <el-input
          v-model="queryParams.carType"
          placeholder="请输入汽车类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务类型" prop="serviceType">
        <el-select
          v-model="queryParams.serviceType"
          placeholder="请选择服务类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.cmi_service_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.cmi_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="预约时间" prop="appointmentTime">
        <el-date-picker
          clearable
          v-model="daterangeAppointmentTime"
          placeholder="请选择预约时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 340px"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="到店时间" prop="arrivalTime">
        <el-date-picker
          clearable
          v-model="daterangeArrivalTime"
          placeholder="请选择到店时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 340px"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['workflow:appointmentInfo:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['workflow:appointmentInfo:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="appointmentInfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        :show-overflow-tooltip="true"
        label="客户姓名"
        align="center"
        prop="customerName"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        label="联系方式"
        align="center"
        prop="customerPhone"
        width="120px"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        label="预约时间"
        align="center"
        prop="appointmentTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.appointmentTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        label="到店时间"
        align="center"
        prop="arrivalTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.arrivalTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        width="100px"
        :show-overflow-tooltip="true"
        label="车牌号码"
        align="center"
        prop="licensePlate"
      />
      <el-table-column
        width="100px"
        :show-overflow-tooltip="true"
        label="汽车类型"
        align="center"
        prop="carType"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        label="服务类型"
        align="center"
        prop="serviceType"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.cmi_service_type"
            :value="scope.row.serviceType"
          />
        </template>
      </el-table-column>
      <el-table-column
        width="130px"
        :show-overflow-tooltip="true"
        label="备注"
        align="center"
        prop="info"
      />
      <el-table-column
        width="90px"
        :show-overflow-tooltip="true"
        label="状态"
        align="center"
        prop="status"
      >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cmi_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        width="300px"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            :disabled="scope.row.status !== 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['workflow:appointmentInfo:edit']"
            >修改</el-button
          >
          <el-button
            :disabled="scope.row.status !== 0"
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleArrival(scope.row)"
            v-hasPermi="['workflow:appointmentInfo:arrival']"
            >到店</el-button
          >
          <el-button
            :disabled="scope.row.status === 0 || scope.row.status === 2"
            size="mini"
            type="text"
            icon="el-icon-finished"
            @click="handleGenerateStatement(scope.row)"
            v-hasPermi="['workflow:appointmentInfo:generateStatement']"
            >结算单</el-button
          >
          <el-button
            :disabled="scope.row.status !== 0"
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="handleCancel(scope.row)"
            v-hasPermi="['workflow:appointmentInfo:cancel']"
            >取消</el-button
          >
          <el-button
            :disabled="scope.row.status !== 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['workflow:appointmentInfo:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预约信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户姓名" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户姓名" />
        </el-form-item>
        <el-form-item label="联系方式" prop="customerPhone">
          <el-input
            v-model="form.customerPhone"
            placeholder="请输入客户手机号码"
          />
        </el-form-item>
        <el-form-item label="预约时间" prop="appointmentTime">
          <el-date-picker
            clearable
            style="width: 100%"
            v-model="form.appointmentTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择预约时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车牌号码" prop="licensePlate">
          <el-input v-model="form.licensePlate" placeholder="请输入车牌号码" />
        </el-form-item>
        <el-form-item label="汽车类型" prop="carType">
          <el-input v-model="form.carType" placeholder="请输入汽车类型" />
        </el-form-item>
        <el-form-item label="服务类型" prop="serviceType">
          <el-select
            v-model="form.serviceType"
            placeholder="请选择服务类型"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.cmi_service_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="info">
          <el-input
            type="textarea"
            :rows="2"
            v-model="form.info"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAppointmentInfo,
  getAppointmentInfo,
  delAppointmentInfo,
  addAppointmentInfo,
  updateAppointmentInfo,
  arrival,
  cancel,
  generateStatement,
} from "@/api/workflow/appointmentInfo";

export default {
  name: "AppointmentInfo",
  dicts: ["cmi_service_type", "cmi_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约信息表格数据
      appointmentInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        customerPhone: null,
        appointmentTime: null,
        arrivalTime: null,
        licensePlate: null,
        carType: null,
        serviceType: null,
        info: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerName: [
          { required: true, message: "客户不能为空", trigger: "blur" },
        ],
        customerPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "手机号码格式不正确",
            trigger: "blur",
          },
        ],
        appointmentTime: [
          { required: true, message: "预约时间不能为空", trigger: "blur" },
        ],
        licensePlate: [
          { required: true, message: "车牌号码不能为空", trigger: "blur" },
          {
            pattern:
              /^[\u4e00-\u9fa5]{1}[A-Z]{1}[\da-zA-Z]{4}[A-Z0-9挂学警港澳]{1}$/,
            message: "车牌号码格式不正确",
            trigger: "blur",
          },
        ],
        carType: [
          { required: true, message: "汽车类型不能为空", trigger: "blur" },
        ],
        serviceType: [
          { required: true, message: "请选择服务类型", trigger: "blur" },
        ],
      },
      // 预约时间 描述信息时间范围
      daterangeAppointmentTime: [],
      // 到店时间 描述信息时间范围
      daterangeArrivalTime: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //已到店
    handleArrival(scope) {
      arrival(scope.id).then((res) => {
        this.$modal.msgSuccess("客户已到店");
        this.getList();
      });
    },
    //取消预约
    handleCancel(scope) {
      cancel(scope.id).then((res) => {
        this.$modal.msgSuccess("客户已取消预约");
        this.getList();
      });
    },
    //结算单
    handleGenerateStatement(scope) {
      this.$confirm(
        scope.status !== 3 ? "是否生成结算单" : "是否查看结算单",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          //如果是已到店，则生成结算单，
          if (scope.status === 1) {
            generateStatement(scope.id).then((res) => {
              this.$message({
                type: "success",
                message: "生成结算单成功,请查看吧!",
              });
            });
          }
          // 已到店生成结算单后跳转到结算单明细。
          // 如果是已支付或结算单生成，则查看结算单 即 结算单明细。
          // 根据预约表中的id，查询结算单表中的 appointment_info_id 进行查询结算单
          this.$router.push({
            path: "/workflow/item",
            query: { appointmentInfoId: scope.id },
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作!",
          });
        });
    },
    /** 查询预约信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (
        null != this.daterangeAppointmentTime &&
        "" != this.daterangeAppointmentTime
      ) {
        this.queryParams.params["beginAppointmentTime"] =
          this.daterangeAppointmentTime[0];
        this.queryParams.params["endAppointmentTime"] =
          this.daterangeAppointmentTime[1];
      }
      if (
        null != this.daterangeArrivalTime &&
        "" != this.daterangeArrivalTime
      ) {
        this.queryParams.params["beginArrivalTime"] =
          this.daterangeAppointmentTime[0];
        this.queryParams.params["endArrivalTime"] =
          this.daterangeAppointmentTime[1];
      }
      listAppointmentInfo(this.queryParams).then((response) => {
        this.appointmentInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerName: null,
        customerPhone: null,
        appointmentTime: null,
        arrivalTime: null,
        licensePlate: null,
        carType: null,
        serviceType: null,
        info: null,
        status: 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAppointmentInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAppointmentInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppointmentInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除预约信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAppointmentInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "workflow/appointmentInfo/export",
        {
          ...this.queryParams,
        },
        `appointmentInfo_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
